<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: white;
            color: #333;
            width: 400px;
            height: 300px;
            overflow: hidden;
            margin: 0;
            padding: 0;
        }

        .iframe-container {
            width: 100%;
            height: 100%;
            padding: 8px;
            background: white;
        }

        .file-list {
            list-style: none;
            height: 100%;
            overflow: hidden;
        }

        .file-item {
            display: flex;
            align-items: center;
            padding: 6px 8px;
            margin-bottom: 4px;
            background: #f8f9fa;
            border-radius: 3px;
            transition: all 0.2s ease;
        }

        .file-item:hover {
            background: #e3f2fd;
            transform: translateX(2px);
        }

        .file-arrow {
            margin-right: 8px;
            color: #1e3a8a;
            font-size: 12px;
            flex-shrink: 0;
            font-weight: bold;
            opacity: 0.8;
        }

        .file-name {
            flex: 1;
            color: #333;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            text-decoration: none;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            min-width: 0;
        }

        .file-name:hover {
            color: #1e3a8a;
        }

        .file-time {
            color: #666;
            font-size: 14px;
            margin-left: 8px;
            flex-shrink: 0;
        }

        .empty-state {
            text-align: center;
            padding: 80px 20px;
            color: #666;
        }

        .empty-state .icon {
            font-size: 48px;
            color: #ccc;
            margin-bottom: 15px;
        }

        .loading {
            text-align: center;
            padding: 80px 20px;
            color: #666;
        }


    </style>
</head>
<body>
    <div class="iframe-container">
        <ul class="file-list" id="fileList">
            <li class="loading">加载中...</li>
        </ul>
    </div>

    <script>
        // 获取前5条文件数据
        async function loadIframeFiles() {
            try {
                // 获取当前页面的协议和域名
                const currentOrigin = window.location.origin;
                const apiUrl = `${currentOrigin}/public/files?limit=10`;
                
                const response = await fetch(apiUrl, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    },
                    mode: 'cors',
                    credentials: 'same-origin'
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                
                if (data.files && data.files.length > 0) {
                    displayIframeFiles(data.files);
                } else {
                    showEmptyState();
                }
            } catch (error) {
                console.error('加载文件失败:', error);
                showError(`加载失败: ${error.message}`);
            }
        }

        // 显示文件列表
        function displayIframeFiles(files) {
            const fileList = document.getElementById('fileList');
            
            const fileItems = files.map(file => {
                // 移除文件后缀
                const fileNameWithoutExt = removeFileExtension(file.filename);
                
                return `
                    <li class="file-item">
                        <span class="file-arrow">></span>
                        <a href="${window.location.origin}/public/download/${file.id}" class="file-name" target="_blank" download title="${file.filename}">
                            ${fileNameWithoutExt}
                        </a>
                        <span class="file-time">${formatDate(file.uploadTime)}</span>
                    </li>
                `;
            }).join('');
            
            fileList.innerHTML = fileItems;
        }

        // 显示空状态
        function showEmptyState() {
            const fileList = document.getElementById('fileList');
            fileList.innerHTML = `
                <li class="empty-state">
                    <div class="icon">📁</div>
                    <div>暂无文件</div>
                </li>
            `;
        }

        // 显示错误信息
        function showError(message) {
            const fileList = document.getElementById('fileList');
            fileList.innerHTML = `
                <li class="empty-state">
                    <div class="icon">❌</div>
                    <div>${message}</div>
                    <div style="font-size: 12px; margin-top: 8px; color: #999;">
                        请检查网络连接或联系管理员
                    </div>
                </li>
            `;
        }

        // 移除文件后缀
        function removeFileExtension(filename) {
            if (!filename) return '';
            const lastDotIndex = filename.lastIndexOf('.');
            if (lastDotIndex === -1) return filename;
            return filename.substring(0, lastDotIndex);
        }

        // 格式化日期
        function formatDate(dateString) {
            if (!dateString) return '';
            const date = new Date(dateString);
            return date.toLocaleDateString('zh-CN', { 
                year: 'numeric', 
                month: '2-digit', 
                day: '2-digit' 
            }).replace(/\//g, '-');
        }

        // 页面加载完成后自动加载文件
        document.addEventListener('DOMContentLoaded', loadIframeFiles);

        // 每5分钟自动刷新一次
        setInterval(loadIframeFiles, 5 * 60 * 1000);
    </script>
</body>
</html> 